//记录查询条件用于回显
// var searchParamArr={"code":-1,"param":""};
function findSeats(CertusJsonAjax, CertusFormAjax, $scope) {
    // 资产信息
    var seatColumns = [{
        id: 'companyCode',
        title: '所属公司',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = "";
            if (record.company_code == "HFKJ") {
                content = "浩方科技";
            } else if (record.company_code == "HFXX") {
                content = "浩方信息";
            } else if (record.company_code == "STSBJ") {
                content = "赛特斯北京";
            } else if (record.company_code == "STSGD") {
                content = "赛特斯广东";
            } else if (record.company_code == "STSJT") {
                content = "赛特斯集团";
            } else if (record.company_code == "STSNJ") {
                content = "赛特斯南京";
            } else if(record.company_code=="STSSH"){
                content = "赛特斯上海";
            }else if(record.company_code=="SYB-RX"){
                content = "融合运维事业部";
            };
            return content;
        }
    }, {
        id: 'seatNo',
        title: '座位编号',
        width: 150,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = record.city_location +
                record.building_location + record.floor_location + record.style_id +
                record.seat_id;
            return content;
        }
    }, {
        id: 'seat_profile',
        title: '座位类型',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'ss',
        title: '状态',
        width: 53,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            var status = record.seat_status;
            if (status == 0) {
                content += "空闲";
            } else if (status == 1) {
                content += "占用";
            } else {
                content += "停用";
            }
            return content;
        }
    }, {
        id: 'address',
        title: '放置城市',
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            if (record.city_location == "NJ") {
                content = "南京";
            } else if (record.city_location == "SH") {
                content = "上海";
            } else if (record.city_location == "BJ") {
                content = "北京";
            } else {
                content = "广东";
            }
            return content;
        }
    }, {
        id: 'building_location',
        title: '大楼栋数',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'floor_location',
        title: '放置楼层',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'room_location',
        title: '放置房间',
        type: 'string',
        columnClass: 'text-center'
    }, {
        id: 'owner_name',
        title: '归属者',
        width: 73,
        type: 'string',
        columnClass: 'text-center',
    }, {
        id: 'comments',
        title: '描述',
        type: 'string',
        columnClass: 'text-left',
    }, {
        id: 'operation',
        title: '操作',
        width: 130,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record) {
            var content = '';
            content += '<button class="btn btn-xs btn-success" onclick="blSeat(\'' + record.id + '\');"><i class="icon-exchange"></i>  待办</button>';
            // if ($scope.isCrud) {
            //     content += '&nbsp;&nbsp;';
            //     content += '<button class="btn btn-xs btn-danger" onclick="delSeat(\'' + record.id + '\');"><i class="fa fa-trash-o"></i>  删除</button>';
            // }
            return content;
        }
    }];

    this.search = function (isTrue) {
        var seatStatus = $("#seatStatus").val();
        var userId = $("#userId").val();

        var buildingLocation = $("#buildingLocation").val();
        var floorLocation = $("#floorLocation").val();
        var seatNo = $("#seatNo").val();
        var cityLocation = $("#cityLocation").val();
        var compCode = $("#compCode").val();

        $("#assetsContainer").empty();
        $("#assetsToolBarContainer").empty();
        CertusFormAjax.ajax('findSeatLists', {
            "seatStatus": seatStatus,
            "userId": userId,
            "buildingLocation": buildingLocation,
            "floorLocation": floorLocation,
            "seatNo": seatNo,
            "cityLocation": cityLocation,
            "isGly": isTrue,
            "compCode": compCode
        }, function (result) {
            var telOption = {
                lang: 'zh-cn',
                ajaxLoad: false,
                extraWidth: 20,
                // loadURL:'url',
                exportFileName: '座位列表',
                datas: result.data,
                columns: seatColumns,
                gridContainer: 'assetsContainer',
                toolbarContainer: 'assetsToolBarContainer',
                tools: '',
                pageSize: 10,
                pageSizeLimit: [10, 15, 20]
            };
            var telGrid = $.fn.dlshouwen.grid.init(telOption);
            telGrid.load();
        });
    };
    CertusFormAjax.ajax('companyListByViewCode', {}, function (response) {
        $scope.comps = JSON.parse(response.data);
    });

    CertusFormAjax.ajax("isCrcd", {
        "code": "ZWGL"
    }, function (response) {
        var data = JSON.parse(response.data);
        if (data.isTrue == true) {
            $scope.isCrud = true;
        } else {
            $scope.isCrud = false;
        }
        search($scope.isCrud);
    });
    this.blSeat = function (id) {
        location.href = "#/toBlSeat/" + id;
    };

    this.editSeat = function (id) {
        location.href = "#/toEditSeat/" + id;
    };

    this.delSeat = function (id) {
        layer.confirm('是否确认删除？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            //删除
            CertusFormAjax.ajax("delSeat", {
                "id": id
            }, function (response) {
                layer.msg("删除成功");
                search();
            });
        });
    };

    //归属人
    CertusFormAjax.ajax("findSpUserList", {}, function (response) {
        var html1 = '<input type="text" value="" id="autocomplete" class="form-control"/>' +
            '<input type="hidden" value="" id="searchParam"/>';
        $("#paramt").html(html1);
        var nhlTeams = [];
        var data = response.data;
        for (var i = 0; i < data.length; i++) {
            var arr = {};
            arr.name = data[i].userName + "[" + data[i].positonName + "]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: {
                    category: obj.id
                }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#userId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });
    $(".psearch").on('click', function () {
        search($scope.isCrud);
    })
}

function toAddAssets(CertusFormAjax, $scope) {
    //所属公司
    CertusFormAjax.ajax("companyListByViewCode", {}, function (response) {
        $scope.comp = JSON.parse(response.data);
    });
    //查询座位类目
    CertusFormAjax.ajax("findCates", {}, function (response) {
        $scope.data = response.data;
    });

    this.change = function () {
        //查询座位类别
        CertusFormAjax.ajax("findCateStyles", {
            "cateId": $("#cateId").val()
        }, function (response) {
            $scope.style = response.data;
        });
    };
    $(".submit").on('click', function () {
        if (!certusValidateNotnull()) {
            return;
        }
        var arr = $("form").serializeArray();
        var data = {};
        for (var i = 0; i < arr.length; i++) {
            data[arr[i].name] = arr[i].value;
        };
        var seatFile = $('#styleId option:selected').text();
        data.seatFile = seatFile;
        CertusFormAjax.ajax("addSeat", {
            "param": JSON.stringify(data)
        }, function (response) {
            layer.msg("提交成功");
            window.history.back();
        });
    })
}

function toBlSeat(CertusFormAjax, $scope, $routeParams) {
    CertusFormAjax.ajax("isCrcd", {
        "code": "ZWGL"
    }, function (response) {
        var data = JSON.parse(response.data);
        if (data.isTrue == true) {
            $scope.isCrud = true;
        } else {
            $scope.isCrud = false;
        }
    });

    CertusFormAjax.ajax("findSpUserList", {}, function (response) {
        var nhlTeams = [];
        var data = response.data;
        for (var i = 0; i < data.length; i++) {
            var arr = {};
            arr.name = data[i].userName + "[" + data[i].positonName + "]";
            arr.id = data[i].userId;
            nhlTeams.push(arr);
        }
        //自动补全
        var nhl = $.map(nhlTeams, function (obj) {
            return {
                value: obj.name,
                data: {
                    category: obj.id
                }
            };
        });
        // Initialize autocomplete with local lookup:
        $('#autocomplete').devbridgeAutocomplete({
            lookup: nhl,
            minChars: 1,
            onSelect: function (suggestion) {
                $("#userId").val(suggestion.data.category);
            },
            showNoSuggestionNotice: true,
            noSuggestionNotice: 'Sorry, no matching results'
        });
    });

    CertusFormAjax.ajax("findProFileEntity", {
        "id": $routeParams.id
    }, function (response) {
        var data = response.data;
        if (data.companyCode == "HFKJ") {
            $scope.companyCode = "浩方科技";
        } else if (data.companyCode == "HFXX") {
            $scope.companyCode = "浩方信息";
        } else if (data.companyCode == "STSBJ") {
            $scope.companyCode = "赛特斯北京";
        } else if (data.companyCode == "STSGD") {
            $scope.companyCode = "赛特斯广东";
        } else if (data.companyCode == "STSJT") {
            $scope.companyCode = "赛特斯集团";
        } else if (data.companyCode == "STSNJ") {
            $scope.companyCode = "赛特斯南京";
        } else if(data.companyCode=="STSSH"){
            $scope.companyCode = "赛特斯上海";
        }else if(data.companyCode=="SYB-RX"){
            $scope.companyCode = "融合运维事业部";
        };
        $scope.SeatId = data.cityLocation + data.buildingLocation +
            data.floorLocation + data.styleId + data.seatId;
        $scope.seatProFile = data.seatProfile;
        if (data.seatStatus == 0) {
            $scope.seatStatus = "空闲";
        } else if (data.seatStatus == 1) {
            $scope.seatStatus = "占用";
        } else {
            $scope.seatStatus = "停用";
        }
        var city = "";
        if (data.cityLocation == "NJ") {
            city = "南京";
        } else if (data.cityLocation == "SH") {
            city = "上海";
        } else if (data.cityLocation == "BJ") {
            city = "北京";
        } else {
            city = "广东";
        }
        $scope.cityLocation = city;
        $scope.buildingLocation = data.buildingLocation;
        $scope.buildingName = data.buildingName;
        $scope.floorLocation = data.floorLocation;
        $scope.roomLocation = data.roomLocation;
        $scope.ownerName = data.ownerName;
        $scope.comments = data.comments;
        $scope.id = data.id;
    });

    CertusFormAjax.ajax("findByPfId", {
        "seatId": $routeParams.id
    }, function (response) {
        // 资产信息记录
        var astlogColumns = [{
            id: 'actionTime',
            title: '操作时间',
            type: 'date',
            width: 100,
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        }, {
            id: 'operateUserName',
            title: '操作人',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'comments',
            title: '内容',
            type: 'string',
            columnClass: 'text-left',
        }];
        var astlogOption = {
            lang: 'zh-cn',
            ajaxLoad: false,
            extraWidth: 20,
            // loadURL:'url',
            exportFileName: '座位信息记录',
            datas: response.data,
            columns: astlogColumns,
            gridContainer: 'seatlogContainer',
            toolbarContainer: 'seatlogToolBarContainer',
            tools: '',
            pageSize: 10,
            pageSizeLimit: [10, 15, 20]
        };
        var astlogGrid = $.fn.dlshouwen.grid.init(astlogOption);
        astlogGrid.load();
    });
    //停用座位
    this.stopSeat = function () {
        CertusFormAjax.ajax("cgSeatStatus", {
            "id": $scope.id,
            "status": 2,
            "ownerId": "",
            "rmdesc": ""
        }, function (response) {
            if (response.data) {
                layer.msg("停用成功");
                $scope.seatStatus = "停用";
                window.history.back();
            } else {
                layer.msg("停用发生错误");
            }
        });
    };
    //空闲座位
    this.featbox = function () {
        CertusFormAjax.ajax("cgSeatStatus", {
            "id": $scope.id,
            "status": 0,
            "ownerId": "",
            "rmdesc": ""
        }, function (response) {
            if (response.data) {
                layer.msg("成功置为空闲状态");
                $scope.seatStatus = "空闲";
                window.history.back();
            } else {
                layer.msg("发生错误");
            }
        });
    };

    //启用座位
    this.startSeat = function () {
        if ($scope.seatStatus != "停用") {
            layer.msg("当前座位处于空闲或占用状态，不能启用！");
            return;
        }

        CertusFormAjax.ajax("cgSeatStatus", {
            "id": $scope.id,
            "status": 0,
            "ownerId": "",
            "rmdesc": ""
        }, function (response) {
            if (response.data) {
                layer.msg("启用成功");
                $scope.seatStatus = "空闲";
                window.history.back();
            } else {
                layer.msg("启用发生错误");
            }
        });
    };

    this.showbox = function () {
        if ($scope.seatStatus == "停用") {
            layer.msg("当前座位已停用，不能转移!");
            return;
        }
        $(".hidebox").show();
    };
    //转移座位
    this.cgAsset = function () {
        var userId = $("#userId").val();
        var mdesc = $("#mdesc").val();
        CertusFormAjax.ajax("cgSeatStatus", {
            "id": $scope.id,
            "status": 1,
            "ownerId": userId,
            "rmdesc": mdesc
        }, function (response) {
            if (response.data||response.data=="true") {
                layer.msg("转移成功");
                window.history.back();
            } else {
                layer.msg("转移发生错误");
            }
        });
    };
}

function toEditSeat(CertusFormAjax, $scope, $routeParams) {
    //所属公司
    CertusFormAjax.ajax("companyListByViewCode", {}, function (response) {
        $scope.comp = JSON.parse(response.data);
    });
    //查询座位类目
    CertusFormAjax.ajax("findCates", {}, function (response) {
        $scope.data = response.data;
    });

    this.change = function () {
        //查询座位类别
        CertusFormAjax.ajax("findCateStyles", {
            "cateId": $("#cateId").val()
        }, function (response) {
            $scope.style = response.data;
        });
    };

    CertusFormAjax.ajax("findProFileEntity", {
        "id": $routeParams.id
    }, function (response) {
        var data = response.data;
        $scope.seatId = data.seatId;
        $scope.companyCode = data.companyCode;
        $scope.styleId = data.styleId;
    });

    $(".submit").on('click', function () {
        if (!certusValidateNotnull()) {
            return;
        }
        var arr = $("form").serializeArray();
        var data = {};
        for (var i = 0; i < arr.length; i++) {
            data[arr[i].name] = arr[i].value;
        };
        var seatFile = $('#styleId option:selected').text();
        data.seatFile = seatFile;
        data.id = $scope.id;
        CertusFormAjax.ajax("updateCates", {
            "param": JSON.stringify(data)
        }, function (response) {
            $scope.data = response.data;
        });
    });
}


function lfimg(start) {
    var json = {
        "title": "工位图", //相册标题
        "id": 1, //相册id
        "start": start, //初始显示的图片序号，默认0
        "data": [ //相册包含的图片，数组格式
            {
                "alt": "南京18栋1楼工位图",
                "pid": 0, //图片id
                "src": "img/18-1.jpg", //原图地址
                "thumb": "" //缩略图地址
            },
            {
                "alt": "南京18栋2楼工位图",
                "pid": 1, //图片id
                "src": "img/18-2.jpg", //原图地址
                "thumb": "" //缩略图地址
            },
            {
                "alt": "南京18栋3楼工位图",
                "pid": 2, //图片id
                "src": "img/18-3.jpg", //原图地址
                "thumb": "" //缩略图地址
            },
            {
                "alt": "南京18栋4楼工位图",
                "pid": 3, //图片id
                "src": "img/18-4.jpg", //原图地址
                "thumb": "" //缩略图地址
            },
            {
                "alt": "南京9栋1楼工位图",
                "pid": 4, //图片id
                "src": "img/9-1.jpg", //原图地址
                "thumb": "" //缩略图地址
            },
        ]
    };
    layer.photos({
        photos: json, //格式见API文档手册页
        anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
    });
}


function selImg() {
    var city = $("#cityLocation").val();
    var build = $("#buildingLocation").val();
    var fool = $("#floorLocation").val();
    //console.log(city, build, fool);
    if (city == "NJ") {
        if (build == "B18" || build == "b18") {
            if (fool == "F01" || fool == "f01") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-1.jpg");
            }
            if (fool == "F02" || fool == "f02") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-2.jpg");
            }
            if (fool == "F03" || fool == "f03") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-3.jpg");
            }
            if (fool == "F04" || fool == "f04") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-4.jpg");
            }
        }
        if (build == "B09" || build == "b09") {
            if (fool == "F01" || fool == "f01") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/9-1.jpg");
            }
        }
    } else {
        $("#lfimg").hide();
    }
};

function getImg() {
    var city = $("#cityLocation").val();
    var build = $("#build").val();
    var fool = $("#fool").val();
    //console.log(city, build, fool);
    if (city == "NJ") {
        if (build == "18") {
            if (fool == "01") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-1.jpg");
            }
            if (fool == "02") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-2.jpg");
            }
            if (fool == "03") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-3.jpg");
            }
            if (fool == "04") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/18-4.jpg");
            }
        }
        if (build == "09") {
            if (fool == "01") {
                $("#lfimg").show();
                $("#imgbox").attr("src", "img/9-1.jpg");
            }
        }
    } else {
        $("#lfimg").hide();
    }
}